Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed sharedBlacklist RegExp due to a bug with Node 12.11.0 #464

Closed
wants to merge 1 commit into from

Conversation

EnduIf
Copy link

@EnduIf EnduIf commented Oct 20, 2019

NodeJs updated in version 12.11.0 to the V8 engine 7.7.7.299.11 (i think the update to the newer V8 is the reason why this is broken) since then the sharedBlacklist regexp was broken due to the function sharedBlacklist() witch in a earlier version from NodeJs returned for the first element in sharedBlacklist this /node_modules[\\\\]react[\\\\]dist[\\\\].*/ output.
In the newer version of NodeJs the output is different /node_modules[\\\]react[\\\]dist[\\\].*/ witch is not valid anymore and it trows a error.

With my little change /node_modules[\/\\]react[\/\\]dist[\/\\].*/ it works again on both versions
Output NodeJs version 12.11.0: /node_modules[\\\\]react[\\\\]dist[\\\\].*/
Output NodeJs version 10.15.2: /node_modules[\\\\]react[\\\\]dist[\\\\].*/

Summary

In the React-Native repo there are many Issues regarding this bug
a few examples
facebook/react-native#26829
facebook/react-native#26598
facebook/react-native#26808

With this small change all the Issues should be fixed

Test plan
Without my Change:
Output NodeJs version 12.11.0: /node_modules[\\\]react[\\\\]dist[\\\].*/
Output NodeJs version 10.15.2: /node_modules[\\\\]react[\\\\]dist[\\\\].*/
With my Change:
Output NodeJs version 12.11.0: /node_modules[\\\\]react[\\\\]dist[\\\\].*/
Output NodeJs version 10.15.2: /node_modules[\\\\]react[\\\\]dist[\\\\].*/

i did not wrote any tests

Other
this is my first pull request for a Facebook product and I've accepted the Facebook CLA

NodeJs updated in version 12.11.0 to the V8 engine 7.7.7.299.11 (i think the update to the newer V8 is the reason why this is broken) since then the sharedBlacklist regexp was broken due to the function sharedBlacklist() witch in a earlier version from NodeJs returned for the first element in sharedBlacklist this `/node_modules[\\\\]react[\\\\]dist[\\\\].*/` output. 
In the newer version of NodeJs the output is different `/node_modules[\\\]react[\\\]dist[\\\].*/` witch is not valid anymore and it trows a error.

With my little change `/node_modules[\/\\]react[\/\\]dist[\/\\].*/` it works again on both versions
Output NodeJs version 12.11.0: `/node_modules[\\\\]react[\\\\]dist[\\\\].*/`
Output NodeJs version 10.15.2: `/node_modules[\\\\]react[\\\\]dist[\\\\].*/`
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 20, 2019
@codecov-io
Copy link

Codecov Report

Merging #464 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #464   +/-   ##
=======================================
  Coverage   84.43%   84.43%           
=======================================
  Files         173      173           
  Lines        5764     5764           
  Branches      949      949           
=======================================
  Hits         4867     4867           
  Misses        794      794           
  Partials      103      103
Impacted Files Coverage Δ
packages/metro-config/src/defaults/blacklist.js 60% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 073eeb0...7d04648. Read the comment docs.

@cpojer
Copy link
Contributor

cpojer commented Oct 29, 2019

Thank you for your PR! Fixed in #468.

@cpojer cpojer closed this Oct 29, 2019
facebook-github-bot pushed a commit that referenced this pull request Oct 29, 2019
Summary:
**Summary**

On Windows with Node.js v12.x.x, Metro crashes with
```
SyntaxError: Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class
```
This has been reported in #453, facebook/react-native#26829, facebook/react-native#26969, facebook/react-native#26878, facebook/react-native#26598, expo/expo-cli#1147 and expo/expo-cli#1074.

There are a few open pull requests attempting to fix this same issue:
* #464
* #461
* #458
* #454

However, none of the existing PRs address the *root cause* of this error: the `escapeRegExp` function in `blacklist.js` tries to convert regular expressions to be agnostic to the path separator ("/" or "\\"), but turns some valid regular expressions to invalid syntax.

The error was is this line:
https://github.com/facebook/metro/blob/142348f5345e40ce2075fc7f9dfa30c5d31fee2a/packages/metro-config/src/defaults/blacklist.js#L28
When given a regular expression, such as `/node_modules[/\\]react[/\\]dist[/\\].*/`, on Windows where `path.sep` is `\` (which is also an escape character in regular expressions), this gets turned into `/node_modules[\\\]react[\\\]dist[\\\].*/`, resulting in the `Unterminated character class` error.

Automatically replacing `[/]` with `[\]` is an error, as is replacing `[\/]` with `[\\]`, because in both of these cases the backslash before the end of character class "]" escapes it, and the character class becomes unterminated. Therefore, this PR changes the code to look for both escaped forward slash `\/` and forward slash `/`, and always replace them with the escaped version (`\/` or `\\`, depending on the platform).

This fixes #453.

**Test plan**

Added a test case that exercises the code with both `\` and `/` as path separators.
Pull Request resolved: #468

Differential Revision: D18201730

Pulled By: cpojer

fbshipit-source-id: 6bb694178314c39d4d6a0fd9f8547bfa2c36f894
@danilocanalle
Copy link

It fixed!

cpojer pushed a commit that referenced this pull request Jan 8, 2020
Summary:
**Summary**

On Windows with Node.js v12.x.x, Metro crashes with
```
SyntaxError: Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class
```
This has been reported in #453, facebook/react-native#26829, facebook/react-native#26969, facebook/react-native#26878, facebook/react-native#26598, expo/expo-cli#1147 and expo/expo-cli#1074.

There are a few open pull requests attempting to fix this same issue:
* #464
* #461
* #458
* #454

However, none of the existing PRs address the *root cause* of this error: the `escapeRegExp` function in `blacklist.js` tries to convert regular expressions to be agnostic to the path separator ("/" or "\\"), but turns some valid regular expressions to invalid syntax.

The error was is this line:
https://github.com/facebook/metro/blob/142348f5345e40ce2075fc7f9dfa30c5d31fee2a/packages/metro-config/src/defaults/blacklist.js#L28
When given a regular expression, such as `/node_modules[/\\]react[/\\]dist[/\\].*/`, on Windows where `path.sep` is `\` (which is also an escape character in regular expressions), this gets turned into `/node_modules[\\\]react[\\\]dist[\\\].*/`, resulting in the `Unterminated character class` error.

Automatically replacing `[/]` with `[\]` is an error, as is replacing `[\/]` with `[\\]`, because in both of these cases the backslash before the end of character class "]" escapes it, and the character class becomes unterminated. Therefore, this PR changes the code to look for both escaped forward slash `\/` and forward slash `/`, and always replace them with the escaped version (`\/` or `\\`, depending on the platform).

This fixes #453.

**Test plan**

Added a test case that exercises the code with both `\` and `/` as path separators.
Pull Request resolved: #468

Differential Revision: D18201730

Pulled By: cpojer

fbshipit-source-id: 6bb694178314c39d4d6a0fd9f8547bfa2c36f894
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants